<template>
  <div id="GDFile">
    <!-- 图片类上传 -->
    <div v-if="isImg">

        <el-upload v-if="compType==1"
        class="avatar-uploader"
        rel="fileUpload"
        :action="fileUploadURL"
        list-type="picture"
        :on-success="fileSuccess"
        :on-remove="removeFile"
        :before-upload="beforeUpload"
        :file-list="fileUploadList"
        >
        <el-button type="primary" size="medium" round plain>上传<i class="el-icon-upload el-icon--right"></i></el-button>
        <div slot="tip" class="el-upload__tip">只能上传jpeg/jpg/png/gif文件，且不超过{{ size }}MB</div>
        </el-upload>
        <el-image v-else :preview-src-list="showList" v-for="(item,index) in fileUploadList" :key="index" :src="item.url" :title="item.originalFilename" class="avatar" />
    </div>
    <!-- 文件类上传 -->
    <div v-else>
        <el-upload v-if="compType==1"
        rel="fileUpload"
        :action="fileUploadURL"
        :show-file-list="true"
        :on-success="fileSuccess"
        :before-upload="beforeUpload"
        :file-list="fileUploadList"
        :on-remove="removeFile"
        >
        <el-button v-if="compType==1" type="primary" size="medium" round plain>上传<i class="el-icon-upload el-icon--right"></i></el-button>
        </el-upload>
    </div>
  </div>
</template>

<script>
import * as API from '@/api/file.js';
export default {
  name: "GDFile",
  props: {
    size: {
      //可上传文件大小(单位MB)
      type: Number,
      default: 5
    },
    limit: {
      //可上传几张图片
      type: Number,
      default: 1
    },
    fileID: {
      //文件id，回显时使用
      type: String,
      default: null
    },
    compType: {
      //组件模式1：编辑 2：只看
      type: Number,
      default: 1
    },
    isImg:{
        type:Boolean,
        default:true
    } // 是否只上传图片
  },
  data() {
    return {
      //文件上传地址
      fileUploadURL: this.isImg==true?"http://localhost:8200/gdones.server/file/uploadOneImage":"http://localhost:8200/gdones.server/file/uploadOneFile",
      //已上传文件
      fileUploadList: [],
      // 展示文件列表
      showList:[]
    };
  },
  mounted() {
    // if(this.fileID!=null){
    // 	this.getFileData(this.fileID);
    // }
  },
  watch: {
    fileID:{
    	handler(newVal,oldVal){
    		if(newVal!=null){
    			console.dir(newVal);
    			this.getFileData(newVal);
    		}
    	},
    	deep:true,
    	immediate:true
    }
  },
  methods: {
    //文件上传之前
    beforeUpload(file) {

      if(this.fileUploadList.length>=this.limit){
        this.$message.error("文件只能上传【"+this.limit+"】个，不可再进行上传！");
        return false;
      }

       if(this.isImg){
            let type = file.name.substring(file.name.lastIndexOf(".") + 1);
            const types = ["jpeg", "jpg", "png","gif"];
            if (types.indexOf(type) === -1) {
                this.$message.error("图片只能上传jpeg/jpg/png/gif格式！");
                return false;
            }
       }

      if (file.size > this.size * 1000 * 1024) {
        this.$message.error("文件大小不能超过" + this.size + "M");
        return false;
      }
    },
    //文件上传成功回调
    fileSuccess(data) {
      data.name = data.originalFilename;
      this.fileUploadList.push(data);
      this.showList.push(data.url);
      this.$emit("uploadSuccess",this.fileUploadList);
    },
    // 文件移除
    removeFile(file, fileList){
      for(let i in this.fileUploadList){
        if(this.fileUploadList[i].id == file.id){
          this.$delete(this.fileUploadList,i);
          break;
        }
      }

      for(let c in this.showList){
        if(this.showList[c] == file.url){
          this.$delete(this.showList,c);
          break;
        }
      }
      this.$emit("uploadSuccess",this.fileUploadList);
    },
    // 获取文件信息
    getFileData(fileID){

        let arr = fileID.split(",");
        for(let i in arr){
            let fid = arr[i];
            let _this = this;
            API.getFileData(fid)
            .then(function(res){
              res.data.name = res.data.originalFilename;
                _this.fileUploadList.push(res.data);
                _this.showList.push(res.data.url);
            });
        }


    }
  }
};
</script>

<style scoped>
.avatar {
  height: 100%;
  float: left;
  margin-right: 5px;
  border: 0.8px solid rgb(76, 202, 252);
}
.rowFile {
  width: 100px;
}
.seeURL {
  width: 100px;
}

/deep/ .el-upload-dragger {
  background-color: #fff;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  width: 100% !important;
  height: 180px;
  text-align: center;
  cursor: pointer;
  overflow: hidden;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
